/**
 * Description:
 * Class Name:DatabaseMetaDataTest
 * Date:2022/4/10 19:14
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第13章_MySQL数据库和JDBC编程._13_8_分析数据库信息._13_8_1_使用DatabaseMetaData分析数据库信息;

import java.io.FileReader;
import java.sql.*;
import java.util.Properties;

public class DatabaseMetaDataTest {
    private String driver;
    private String url;
    private String user;
    private String pwd;

    public void initParam(String paramFilePath) throws Exception{
        Properties props = new Properties();
        props.load(new FileReader(paramFilePath));
        driver = props.getProperty("driver");
        url = props.getProperty("url");
        user = props.getProperty("user");
        pwd = props.getProperty("pwd");
    }

    public void printResultSet(ResultSet resultSet) throws Exception{
        ResultSetMetaData rsmd = resultSet.getMetaData();   // 获取传入的结果集的MetaData信息对象
        // 打印所有列标题
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            System.out.println(rsmd.getColumnName(i) + "\t");
        }
        System.out.println("\n");

        // 打印全部数据
        while (resultSet.next()){
            for (int i = 1; i < rsmd.getColumnCount(); i++) {
                System.out.println(resultSet.getString(i) + "\t");
            }
            System.out.println("\n");
        }
        resultSet.close();
    }

    public void info() throws Exception{
        try (
                Connection connection = DriverManager.getConnection(url, user, pwd);
                ){
            // 获取DatabaseMetaData对象
            DatabaseMetaData databaseMetaData = connection.getMetaData();
            System.out.println(databaseMetaData.supportsBatchUpdates());

            // 获取MySQL支持的所有表类型
            ResultSet rs = databaseMetaData.getTableTypes();
            System.out.println("--MySQL支持的表类型信息--");
            printResultSet(rs);

            // 获取当前数据库的全部数据表
            rs = databaseMetaData.getTables("select_test", null, "%", new String[]{"TABLES"});
            System.out.println("--当前数据库里的数据表信息--");
            printResultSet(rs);

            // 获取student_table表的主键
            rs = databaseMetaData.getPrimaryKeys("select_test", null, "student_table");
            System.out.println("--student_table表的主键信息--");
            printResultSet(rs);

            // 获取当前数据库的全部存储过程
            rs = databaseMetaData.getProcedures("select_test", null, "%");
            System.out.println("--当前数据库里的存储过程信息--");
            printResultSet(rs);

            // 获取teacher_table 和 student_table 表之间的外键约束
            rs = databaseMetaData.getCrossReference("select_test", null, "teacher_table", null, null, "student_table");
            System.out.println("--teacher_table 和 student_table 表之间的外键约束--");
            printResultSet(rs);

            // 获取student_table表的全部数据列
            rs = databaseMetaData.getColumns("select_test", null, "student_table", "%");
            System.out.println("--student_table表的全部数据列--");
            printResultSet(rs);
        }
    }

    public static void main(String[] args) throws Exception{
        var dmdt = new DatabaseMetaDataTest();
        dmdt.initParam("Chapter_13_mysql.ini");
        Class.forName(dmdt.driver);

        dmdt.info();
    }
}
